package com.kehutong.crm.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期类
 *
 * @author wupengfei
 */
public abstract class DateUtils {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 获取当前周的周一的日期
     *
     * @param date 传入日期
     * @return 当前周的周一的日期
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day);
        return calendar.getTime();
    }

    /**
     * 按指定的格式格式化日期
     *
     * @param date    日期
     * @param pattern 格式
     * @return 格式化日期
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

}
